/**
 * 店铺业主关联管理API接口
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */

import api from './request'
import type { ApiResponse } from '@/types'

// 店铺业主关联查询参数接口
export interface ShopOwnerRelationQueryParams {
  current?: number
  size?: number
  shopId?: number
  ownerId?: number
  relationType?: number
  status?: number
}

// 店铺业主关联表单数据接口
export interface ShopOwnerRelationFormData {
  id?: number
  shopId: number
  ownerId: number
  relationType: number
  ownershipRatio?: number
  leaseStartDate?: string
  leaseEndDate?: string
  rentPrice?: number
  depositAmount?: number
  contractNumber?: string
  contractFile?: string
  isPrimary?: number
  status?: number
  remark?: string
}

// 店铺业主关联详情接口
export interface ShopOwnerRelationDetail {
  id: number
  shopId: number
  shopName: string
  ownerId: number
  ownerName: string
  ownerPhone: string
  ownerType: number
  ownerTypeName: string
  companyName?: string
  relationType: number
  relationTypeName: string
  ownershipRatio?: number
  leaseStartDate?: string
  leaseEndDate?: string
  rentPrice?: number
  depositAmount?: number
  contractNumber?: string
  contractFile?: string
  isPrimary: number
  status: number
  statusName: string
  remark?: string
  createTime: string
  updateTime: string
}

// 分页响应接口
export interface ShopOwnerRelationPageResponse {
  records: ShopOwnerRelationDetail[]
  total: number
  current: number
  size: number
}

/**
 * 店铺业主关联管理API
 */
export const shopOwnerRelationApi = {
  /**
   * 分页查询店铺业主关联列表
   * @param params 查询参数
   * @returns 分页结果
   */
  getShopOwnerRelationPage: (params: ShopOwnerRelationQueryParams) => {
    return api.get<ApiResponse<ShopOwnerRelationPageResponse>>('/api/region/shop-owner-relation/page', { params })
  },

  /**
   * 根据ID获取店铺业主关联详情
   * @param id 关联ID
   * @returns 关联详情
   */
  getShopOwnerRelationDetail: (id: number) => {
    return api.get<ApiResponse<ShopOwnerRelationDetail>>(`/api/region/shop-owner-relation/${id}`)
  },

  /**
   * 根据店铺ID获取业主列表
   * @param shopId 店铺ID
   * @returns 业主列表
   */
  getOwnersByShopId: (shopId: number) => {
    return api.get<ApiResponse<ShopOwnerRelationDetail[]>>(`/api/region/shop-owner-relation/shop/${shopId}/owners`)
  },

  /**
   * 根据业主ID获取关联店铺列表
   * @param ownerId 业主ID
   * @returns 店铺列表
   */
  getShopsByOwnerId: (ownerId: number) => {
    return api.get<ApiResponse<ShopOwnerRelationDetail[]>>(`/api/region/shop-owner-relation/owner/${ownerId}/shops`)
  },

  /**
   * 创建店铺业主关联
   * @param data 关联信息
   * @returns 操作结果
   */
  createShopOwnerRelation: (data: ShopOwnerRelationFormData) => {
    return api.post<ApiResponse<number>>('/api/region/shop-owner-relation', data)
  },

  /**
   * 更新店铺业主关联
   * @param id 关联ID
   * @param data 关联信息
   * @returns 操作结果
   */
  updateShopOwnerRelation: (id: number, data: Partial<ShopOwnerRelationFormData>) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop-owner-relation/${id}`, data)
  },

  /**
   * 删除店铺业主关联
   * @param id 关联ID
   * @returns 操作结果
   */
  deleteShopOwnerRelation: (id: number) => {
    return api.delete<ApiResponse<boolean>>(`/api/region/shop-owner-relation/${id}`)
  },

  /**
   * 批量设置店铺业主关联
   * @param data 关联列表
   * @returns 操作结果
   */
  batchSetShopOwnerRelations: (data: ShopOwnerRelationFormData[]) => {
    return api.post<ApiResponse<boolean>>('/api/region/shop-owner-relation/batch', data)
  },

  /**
   * 获取店铺主要联系人
   * @param shopId 店铺ID
   * @returns 主要联系人信息
   */
  getPrimaryContactByShopId: (shopId: number) => {
    return api.get<ApiResponse<ShopOwnerRelationDetail>>(`/api/region/shop-owner-relation/shop/${shopId}/primary-contact`)
  },

  /**
   * 检查店铺业主关联是否存在
   * @param shopId 店铺ID
   * @param ownerId 业主ID
   * @returns 是否存在
   */
  checkShopOwnerRelationExists: (shopId: number, ownerId: number) => {
    return api.get<ApiResponse<boolean>>(`/api/region/shop-owner-relation/exists`, {
      params: { shopId, ownerId }
    })
  }
}
